import requests
import simplejson
import time
import os
import openai

from model import Model
from utils import LOG
from openai import OpenAI

MOONSHOT_BASE_URL = "https://api.moonshot.cn/v1"

class MoonshotModel(Model):
    def __init__(self, model: str, api_key: str):
        self.model = model
        self.client = OpenAI(
            api_key = api_key,
            base_url = MOONSHOT_BASE_URL,
        )

    def make_request(self, prompt):
        try:
            if self.model in ["moonshot-v1-8k", "moonshot-v1-32k", "moonshot-v1-128k",] :
                response = self.client.chat.completions.create(
                    model=self.model,
                    messages = [
                        {"role": "system", "content": "你是 Kimi，由 Moonshot AI 提供的人工智能助手"},
                        {"role": "user", "content": prompt}
                    ],
                )
                translation = response.choices[0].message.content.strip()

            return translation, True

        except Exception as e:
            raise Exception(f"发生了未知错误：{e}")

        return "", False
